
<?php

$commodityid = $_GET["commodityid"];
$bag = $_GET["bag"];
$box = $_GET["box"];
$name = $_GET["name"];
$buyer = $_GET["buyer"];
$buyerid = $_GET["buyerid"];
$sellprice = $_GET["sellprice"];   //一件多少钱
$total = $_GET["total"];            //总共多少钱
$date = $_GET["date"];
$subdate = $_GET["subdate"];
$openid = $_GET["openid"];


$conn = mysqli_connect("localhost", "root", "", "cloudwarehouse");

if (!$conn)
    echo "数据库连接失败！";
else {
    $sql_getUnit = "SELECT commodityUnit_conversion FROM commodity WHERE commodityId = '$commodityid'";
    $result_unit = mysqli_query($conn,$sql_getUnit);
    $unit =  mysqli_fetch_row($result_unit);
    $amount = $box * $unit[0] + $bag;                       //amount为总bag数量

    // echo $amount;        测试用

    $sql_outbound = "INSERT INTO outboundlist (outbound_commodityId,outbound_commodityName,outboundBag,outboundPrice,outboundTotalprice,outboundBuyer,outbound_buyerId,outboundDate,operationTime,openid) VALUES ('$commodityid','$name','$amount','$sellprice','$total','$buyer','$buyerid','$date','$subdate','$openid') ";
    $result_outbound = mysqli_query($conn,$sql_outbound);        //创建出库表


    $sql_modify = "UPDATE warehouse SET wh_commodityBag = wh_commodityBag - '$amount' WHERE wh_commodityId = '$commodityid'";
    $result_modify = mysqli_query($conn,$sql_modify);               //更新仓库数量

    $sql_cost = "SELECT commodityCost FROM commodity WHERE commodityId = '$commodityid'";
    $result_cost = mysqli_query($conn,$sql_cost);
    $costarray =  mysqli_fetch_row($result_cost);
    $cost = $amount * $costarray[0];                                //成本
    $profit = $total - $cost;

    $sql_profit = "INSERT INTO commodity_profit (outboundDate,cost_price,sell_price,profit,openid) VALUES ('$date','$cost','$total','$profit','$openid')";
    $result_profit = mysqli_query($conn,$sql_profit);               //记录利润表


    $sql_search_dp = "SELECT buyerPrice FROM buyer_defaultprice WHERE buyerId = '$buyerid' AND buyer_CommodityID = '$commodityid' ";
    // $result_search_dp = mysqli_query($conn,$sql_search_dp);
    
    if(mysqli_fetch_row(mysqli_query($conn,$sql_search_dp)) < 1)
    {
        echo "没有这个人的数据" ;

        $sql_insert = "INSERT INTO buyer_defaultprice (buyerId,buyer_CommodityID,buyerPrice) VALUES ('$buyerid','$commodityid','$sellprice')";
        $result_insert = mysqli_query($conn,$sql_insert); 
    }
    else    {
        echo "有这个人的数据";
    }


    // if($result_outbound < 1 || $result_modify < 1 )
    //     echo "更新失败";
    // else
    //     echo $commodityid;

    //这些是判断是否执行成功的语句，我没弄明白，先搁置一下。
}
?>